<cfparam name="CSAC_Order_ID" type="integer" default="0" />
<cfparam name="Organization_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="orders" />
<cfparam name="passback" type="string" default="" />
<cfparam name="detail" type="boolean" default="false" />

<cfscript>
	base = new assets.calgrants.com.Base();
	passbackQueryString = base.passbackToQueryString(passback);
	csacOrderGateway = new assets.calgrants.com.gateway.CSACOrderGateway();
	csacOrder = csacOrderGateway.get(CSAC_Order_ID = CSAC_Order_ID);
	if (csacOrder.getCSAC_Order_ID() eq 0 AND request.authUser.organizationID gt 0) {
		csacOrder.setOrganization_ID(request.authUser.organizationID);
	} else if (Organization_ID gt 0) {
		csacOrder.setOrganization_ID(Organization_ID);
	}
</cfscript>

<cfif csacOrder.getOrganization_ID() eq 0>

	<cfset returnView = view />
	<cfset cancelView = returnView />
	<cfset passback = "CSAC_Order_ID,0" />
	<cfinclude template="/assets/calgrants/ui/forms/selectOrganization.cfm">

<cfelseif csacOrder.getOrganization_ID() gt 0 and IsNumeric(csacOrder.getCSAC_Order_ID())>

	<cfscript>
		if ((IsUserInRole("Organizer") or IsUserInRole("Site Contact")) and csacOrder.getCSAC_Order_ID() eq 0) {
			useScreens = true;
		} else {
			useScreens = false;
		}		
		// Get workshops for order
		workshopPeriod = "All Future";
		workshopGateway = new assets.calgrants.com.gateway.WorkshopGateway();
		if (csacOrder.getCSAC_Order_ID() eq 0) {
			qWorkshops = workshopGateway.query(Program_Year = csacOrder.getProgram_Year(), Organization_ID = csacOrder.getOrganization_ID(), period = workshopPeriod);
			allWorkshopIDs = ValueList(qWorkshops.Workshop_ID);
			if (IsUserInRole("Super User") or IsUserInRole("Regional Admin")) {
				orderWorkshopIDs = allWorkshopIDs;  // req
			} else {
				// todo: swap out check against CSAC_Order.Workshop_IDs to lookup in CSAC_Order_Workshop.Workshop_ID
				qAllCSACOrders = csacOrderGateway.query(Program_Year = csacOrder.getProgram_Year(), Organization_ID = csacOrder.getOrganization_ID(), Deleted = 0);
				usedWorkshopIDs = ValueList(qAllCSACOrders.Workshop_IDs);
				orderWorkshopIDs = ""; // req
				if (ListLen(usedWorkshopIDs) gt 0) {
					for (i=1; i lte ListLen(allWorkshopIDs); i++) {
						wsid = ListGetAt(allWorkshopIDs, i);
						if (ListFind(usedWorkshopIDs, wsid) eq 0) {
							orderWorkshopIDs = ListAppend(orderWorkshopIDs, wsid);
						}
					}
				} else {
					orderWorkshopIDs = allWorkshopIDs; // req
				}
			}
		} else {
			qWorkshops = workshopGateway.query(workshopIDs = csacOrder.getWorkshop_IDs());
			orderWorkshopIDs = ValueList(qWorkshops.Workshop_ID); // req
		}
	</cfscript>
	
	<cfif ListLen(orderWorkshopIDs) gt 0>

		<cfquery name="qOrderWorkshops" dbtype="query">
			select * from qWorkshops where Workshop_ID in (#orderWorkshopIDs#)
		</cfquery>
		<cfquery name="getWorkshopTotals" dbtype="query">
			select Sum(Anticipated_Students) as totalAnticipatedStudents, Sum(Anticipated_Volunteers) as totalAnticipatedVolunteers, Sum(Anticipated_Computers) as totalAnticipatedComputers, Min(Anticipated_Volunteers) as minimumUniqueVolunteers from qOrderWorkshops
		</cfquery>
		<cfscript>
			// set Total... and Unique_Volunteers
			totalAnticipatedStudents = getWorkshopTotals.totalAnticipatedStudents;
			totalAnticipatedVolunteers = getWorkshopTotals.totalAnticipatedVolunteers;
			totalAnticipatedComputers = getWorkshopTotals.totalAnticipatedComputers;
			minimumUniqueVolunteers = getWorkshopTotals.minimumUniqueVolunteers;
			
			// Load up WMS objects
			organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
			organization = organizationGateway.get(Organization_ID = csacOrder.getOrganization_ID());
			userGateway = new assets.calgrants.com.gateway.UserGateway();
			if (csacOrder.getCSAC_Order_ID() eq 0) {
				orderedBy = userGateway.get(User_ID = request.authUser.userID);
			} else {
				orderedBy = userGateway.get(User_ID = csacOrder.getOrdered_By_ID());
			}
			// Load up CSAC objects
			csacBase = new assets.csac.com.Base();
			statuses = csacBase.getOrderStatuses();
			institutionTypes = csacBase.getInstitutionTypes();

			csacGateway = new assets.csac.com.gateway.Gateway();
			institutions = csacGateway.call(objType = "view", obj = "getInstitutions");
			highSchools = csacGateway.call(objType = "view", obj = "getHighSchools");
			cities = csacGateway.call(objType = "view", obj = "getCities");
			
			orderProductGateway = new assets.csac.com.gateway.OrderProductGateway();
			productGateway = new assets.csac.com.gateway.ProductGateway();
			qOrderProducts = orderProductGateway.query(Order_ID = csacOrder.getOrder_ID(), Deleted = 0);
			orderProductProductIDs = ValueList(qOrderProducts.Product_ID);
			qProducts = productGateway.query(Active = 1, productCategoryIDs = "1,6,7,9,10");

			trackingGateway = new assets.csac.com.gateway.TrackingGateway();
			qTrackings = trackingGateway.query(Deleted = 0, Order_ID = csacOrder.getOrder_ID());
			
			// new order defaults - neccessary??
			if (csacOrder.getCSAC_Order_ID() eq 0) {
				// WMS.CSAC_Order
				csacOrder.setProgram_Year(request.currentProgramYear);
				csacOrder.setOrdered_By_ID(GetAuthUser());
				csacOrder.setWorkshop_IDs("");
				csacOrder.setUnique_Volunteers(0);
				// CSAC.Order
				csacOrder.setStatus(statuses[1].value);
				csacOrder.setApproved_By_ID(0);
				csacOrder.setInstitution_ID(0);
				csacOrder.setFirst_Name(orderedBy.getFirst_Name());
				csacOrder.setLast_Name(orderedBy.getLast_Name());
				csacOrder.setEmail(orderedBy.getEmail());
				csacOrder.setPhone(orderedBy.getPhone());
				csacOrder.setExtension(orderedBy.getExtension());
				csacOrder.setWMS_Order_ID(0);
			}
		</cfscript>
		<script src="/assets/jquery.validator/dist/jquery.validate.min.js" type="text/javascript"></script>
		<script src="/assets/jquery.validator/dist/additional-methods.min.js" type="text/javascript"></script>
		<script type="text/javascript" language="javascript">
		<!--
			<cfoutput>
				var productQuantityRules = {};
				var useScreens = #useScreens#;
				var userRole = "#LCase(GetUserRoles())#";
			</cfoutput>
			var currentScreen = 1;
			var screenCount = 3;
			var validatorConfig = {
				debug: false,
				rules: {
					CSAC_Order_ID: {required: true, digits: true, min: 0},
					Order_ID: {required: true, digits: true, min: 0},
					Organization_ID: {required: true, digits: true, min: 0},
					Ordered_By_ID: {required: true, digits: true, min: 0},
					Institution_ID: {required: true, digits: true, min: 0},
					Status: {required: true},
					First_Name: {required: true},
					Last_Name: {required: true},
					Email: {required: true, email: true},
					Phone: {required: true, phoneUS: true},
					Institution_Type: {required: true},
					College_University_ID: {
						required: {depends: function(elements) {return $("#Institution_Type").val() == "College University"}}
					},
					High_School_ID: {
						required: {depends: function(elements) {return $("#Institution_Type").val() == "High School"}}
					},
					Institution: {
						required: {depends: function(elements) {return $("#Institution_Type").val() == "Other"}}
					},
					Address1: {required: true},
					City: {required: true},
					Zip_Code: {
						required: true, 
						digits: true, 
						minlength: 5, 
						maxlength: 5
					},
					Workshop_ID: {
						required: {depends: function(elements) {return !useScreens || (useScreens && currentScreen >= 2)}},
					},
					Total_Anticipated_Students: {required: true, digits: true},
					Total_Anticipated_Volunteers: {required: true, digits: true},
					Total_Anticipated_Computers: {required: true, digits: true},
					Unique_Volunteers: {
						required: {depends: function(elements) {return $(".workshop:checked").length > 0}},
						digits: true,
						min: function(elements) {
							var minAnticipatedVolunteers = parseInt($("#Total_Anticipated_Volunteers").val());
							var workshops = $(".workshop:checked");
							for (var i=0; i<workshops.length; i++) {
								var workshop = $(workshops[i]);
								if (parseInt($("#Anticipated_Volunteers_" + workshop.val()).val()) < minAnticipatedVolunteers) {
									minAnticipatedVolunteers = parseInt($("#Anticipated_Volunteers_" + workshop.val()).val());
								}
							}
							return minAnticipatedVolunteers	
						},
						max: function(elements) {return parseInt($("#Total_Anticipated_Volunteers").val());}
					},
					Order_Product_ID: {required: true},
					Product_ID: {required: true}
					// no rules defined for Quantity_<Product_ID>			
				},
				messages: {
					CSAC_Order_ID: "CSAC_Order_ID is required",
					Order_ID: "Order_ID is required",
					Organization_ID: "Organization_ID is required",
					Ordered_By_ID: "Ordered_By_ID is required",
					Institution_ID: "Institution_ID is required is required",
					Status: "Status is required",
					First_Name: "First Name is required",
					Last_Name: "Last Name is required",
					Email: {
						required: "E-mail is required",
						email: "E-mail is invalid"
					},
					Phone: {
						required: "Phone is required",
						phoneUS: "Phone is invalid"
					},
					Institution_Type: "Recipient Organization Type is required",
					College_University_ID: "College\/University is required",
					High_School_ID: "High School is required",
					Institution: "Organization is required",
					Address1: "Address 1 is required",
					City: "City is required",
					Zip_Code: {
						required: "Zip Code is required",
						digits: "Zip Code must be 5 digits",
						minlength: "Zip Code must be 5 digits",
						maxlength: "Zip Code must be 5 digits"
					},
					Workshop_ID: {
						required: "At least one workshop is required to submit an order",
						minlength: "At least one workshop must be selected"
					},
					Total_Anticipated_Students: {
						required: "Total Anticipated Students is required",
						digits: "Total Anticipated Students must be a positive number"
					},
					Total_Anticipated_Volunteers: {
						required: "Total Anticipated Volunteers is required",
						digits: "Total Anticipated Volunteers must be a positive number"
					},
					Total_Anticipated_Computers: {
						required: "Total Anticipated Computers is required",
						digits: "Total Anticipated Computers must be a positive number"
					},
					Unique_Volunteers: {
						required: "Unique Volunteers is required",
						digits: "Unique Volunteers must be a postitive number",
						min: "Unique Volunteers cannot be less than the smallest value for Anticipated Volunteers ({0})",
						max: "Unique Volunteers cannot be more than Total Anticipated Volunteers ({0})",
					},
					Order_Product_ID: "Order_Product_ID is required",
					Product_ID: "Product_ID is required"
				},
				onsubmit: true,
				onfocusout: false,
				onkeyup: false,
				onclick: false,
				submitHandler: function(form) {
					var valid = true;
					var errorMessages = [];
					var products = $(".productID");
					var totalQuantity = 0;
					var totalTShirts = 0;
					for (var p=0; p<products.length; p++) {
						var product = $(products[p]);
						var productID = product.val();
						var quantity = parseInt($("#Quantity_" + productID).val());
						if (!isInt(quantity)) {
							valid = false;
							errorMessages.push("Quantity is required for every item - enter 0 for none");
						}
						if (isInt(quantity) && quantity > 0) {
								var productQuantityRule = productQuantityRules[productID];
								if (productQuantityRule.SUBCATEGORY == "Volunteer T-Shirts") {
									totalTShirts = totalTShirts + quantity;
									if (isInt(parseInt(productQuantityRule.MAXIMUM)) && totalTShirts > parseInt(productQuantityRule.MAXIMUM)) {
										valid = false;
										errorMessages.push("Total quantity for Cash for College - Volunteer T-Shirts is over the maximum of " + parseInt(productQuantityRule.MAXIMUM));	
									}
								} else {
									if (isInt(productQuantityRule.MAXIMUM) && quantity > parseInt(productQuantityRule.MAXIMUM)) {
										valid = false;
										errorMessages.push("Quantity for " + productQuantityRule.PRODUCT + " is over the maximum of " + parseInt(productQuantityRule.MAXIMUM));
									}
								}
								if (isInt(productQuantityRule.INCREMENT) > 0 && quantity % parseInt(productQuantityRule.INCREMENT) !== 0) {
									valid = false;
									errorMessages.push("Quantity for " + productQuantityRule.PRODUCT + " is not an increment of " + parseInt(productQuantityRule.INCREMENT));
								}
							totalQuantity = totalQuantity + quantity;
						}
					}
					if (totalQuantity == 0) {
						valid = false;
						errorMessages.push("No items in order");
					}
					if (!valid) {
						var errorMessage = "";
						for (i in errorMessages) {
							errorMessage += errorMessages[i] + "\n";
						}
						if (userRole == "super user") {
							if (confirm("Hello Despina\!\n\nThe items listed below are over the maximum or not in the proper increment.\n\nIf you click Ok, the form will submit anyway.  If you click Cancel, the form will not submit and you change the item quantities.\n\n" + errorMessage)) {
								form.submit();
							}
						} else {
							alert(errorMessage);
						}
					} else {
						form.submit();
					}
				},
				showErrors: function(errorMap, errorList) {
					// triggered whenever form is validated
					var errorCount = errorList.length;
					if (errorCount) {
						var errorMessage = "";
						for (i in errorMap) {
							errorMessage += errorMap[i] + "\n";
						}
						alert(errorMessage);
					}
				}
			};
			var initScreen = function() {
				$(".screen").hide();
				if (useScreens) {
					$(".screen1").show();
				} else {
					$(".screen1").show();
					$(".screen2").show();
					$(".screen3").show();
				}
				showHideButtons();
			}
			var nextScreen = function() {
				if (!$("#nextButton").hasClass("disabled") && $("#csacOrderForm").valid()) {
					var nextScreen = currentScreen + 1;
					$(".screen").hide();
					$(".screen" + nextScreen).show();
					currentScreen = nextScreen;
					showHideButtons();
				} else if ($("#nextButton").hasClass("diabled")) {
					alert("Correct the values to go to the next step");
				}
			}
			var previousScreen = function() {
				var previousScreen = currentScreen - 1;
				$(".screen").hide();
				$(".screen" + previousScreen).show();
				currentScreen = previousScreen;
				showHideButtons();
			}
			var showHideButtons = function() {
				$(".screenButton").hide();
				if (useScreens) {
					if (currentScreen == 1) {
						$("#cancelButton").show();
						$("#nextButton").show();
						//$("#saveButton").show();
					} else if (currentScreen == 2) {
						$("#cancelButton").show();
						$("#backButton").show();
						$("#nextButton").show();
					} else if (currentScreen == 3) {
						$("#cancelButton").show();
						$("#backButton").show();
						$("#saveButton").show();
					} else if (currentScreen == 4) {
						$("#closeButton").show();
					}
				} else {
					$("#cancelButton").show();
					$("#saveButton").show();
				}
			}
			var validateWorkshops = function() {
				var valid = true;
				var invalidWorkshopIDs = [];
				var validWorkshopIDs = [];
				var selectedWorkshopIDs = [];
				var errorMessages = [];
				var totalStudents = 0;
				var totalVolunteers = 0;
				var totalComputers = 0;
				var minVolunteers = 0;
				if ($(".workshop:checked").length == 0) {
					valid = false;
					errorMessages.push("At least one workshop must be selected");
					// ui
					$("#totalAnticipatedStudents").text(0);
					$("#totalAnticipatedVolunteers").text(0);
					$("#totalAnticipatedComputers").text(0);
					// hidden form fields
					$("#Total_Anticipated_Students").val(0);
					$("#Total_Anticipated_Volunteers").val(0);
					$("#Total_Anticipated_Computers").val(0);
					$("#Minimum_Unique_Volunteers").val(0);
				} else {
					$(".workshop").each(function() {
						valid = true;
						var workshopID = $(this).val();
						var anticipatedStudents = $("#Anticipated_Students_" + workshopID);
						var anticipatedVolunteers = $("#Anticipated_Volunteers_" + workshopID);
						var anticipatedComputers = $("#Anticipated_Computers_" + workshopID);
						if (!isInt(anticipatedStudents.val()) || parseInt(anticipatedStudents.val()) < 10) {
							valid = false;
							anticipatedStudents.val(0);
							errorMessages.push("Anticipated Students is required and must be a number greater than or equal to 10");
						}
						if (!isInt(anticipatedVolunteers.val()) || parseInt(anticipatedVolunteers.val()) < 0) {
							valid = false;
							anticipatedVolunteers.val(0);
							errorMessages.push("Anticipated Volunteers is required and must be a number. Enter 0 for none.");
						}
						if (!isInt(anticipatedComputers.val()) || parseInt(anticipatedComputers.val()) < 0) {
							valid = false;
							anticipatedComputers.val(0);
							errorMessages.push("Anticipated Computers is required and must be a number.  Enter 0 for none.");
						}
						if (valid) {
							validWorkshopIDs.push(workshopID);
							if ($(this).is(":checked")) {
								totalStudents = totalStudents + parseInt(anticipatedStudents.val());
								totalVolunteers = totalVolunteers + parseInt(anticipatedVolunteers.val());
								totalComputers = totalComputers + parseInt(anticipatedComputers.val());
								if (minVolunteers == 0) {
									minVolunteers = parseInt(anticipatedVolunteers.val());
								} else if (parseInt(anticipatedVolunteers.val()) < minVolunteers) {
									minVolunteers = parseInt(anticipatedVolunteers.val());	
								}
								selectedWorkshopIDs.push(workshopID);
							}
						} else {
							$(this).prop("checked", false);
							invalidWorkshopIDs.push(workshopID);
						}
					});
					// ui
					$("#totalAnticipatedStudents").text(totalStudents);
					$("#totalAnticipatedVolunteers").text(totalVolunteers);
					$("#totalAnticipatedComputers").text(totalComputers);
					// hidden form fields
					$("#Total_Anticipated_Students").val(totalStudents);
					$("#Total_Anticipated_Volunteers").val(totalVolunteers);
					$("#Total_Anticipated_Computers").val(totalComputers);
					$("#Minimum_Unique_Volunteers").val(minVolunteers);
					// javascript variables
					setProductQuantityRules();
				}
				if (!valid) {
					$("#nextButton").addClass("disabled");
					$("#saveButton").addClass("disabled");
					if (errorMessages.length > 0) {
						var errorMessage = "";
						for (var e=0; e<errorMessages.length; e++) {
							errorMessage += errorMessages[e] + "\n";	
						}
						alert(errorMessage);
					}
				} else {
					$("#nextButton").removeClass("disabled");
					$("#saveButton").removeClass("disabled");
				}
			}
			var setProductQuantityRules = function() {
				var pqrArgs = {};
				//pqrArgs.productIDs = $(".productID").val();
				//console.log($(".productID").val().toString());
				pqrArgs.Workshop_IDs = $("#Workshop_IDs").val();
				pqrArgs.Total_Anticipated_Students = $("#Total_Anticipated_Students").val();
				pqrArgs.Unique_Volunteers = $("#Unique_Volunteers").val(); 
				$.ajax({
					type: "GET",
					url: "/assets/calgrants/com/API.cfc?method=checkStock",
					data: pqrArgs,
					dataType: "json",
					success: function(response) {
						for (productID in response) {
							var ruleContainer = $(".productQuantityRuleContainer_" + productID);
							var ruleMessage = "";
							if (isInt(parseInt(response[productID].MAXIMUM))) {
								if (parseInt(response[productID].MINUMUM) > 0) {
									ruleMessage += "Minimum " + parseInt(response[productID].MINIMUM) + "\<br \/\>";
								}
							}
							if (isInt(parseInt(response[productID].MAXIMUM))) {
								if (response[productID].SUBCATEGORY == "Volunteer T-Shirts") {
									ruleMessage += "Maximum " + parseInt(response[productID].MAXIMUM) + "\<br \/\>total across\<br \/\>all sizes\<br \/\>";
								} else {
									ruleMessage += "Maximum " + parseInt(response[productID].MAXIMUM) + "\<br \/\>";
								}
							}
							if (isInt(parseInt(response[productID].INCREMENT)) && parseInt(response[productID].INCREMENT) > 1) {
								ruleMessage += "Increment of " + parseInt(response[productID].INCREMENT) + "\<br \/\>";
							}
							if (ruleMessage.length > 0) {
								ruleContainer.html(ruleMessage);
								ruleContainer.show();
							}
						}
						productQuantityRules = response;
						return true;
					},
					error: function(jqXHR, textStatus, errorThrown) {
						console.log(errorThrown);
						alert("Error updating quantity rules");
						return false;
					}
				});
			}
			var updateWorkshopIDs = function() {
				var vals = [];
				$(".workshop").each(function() {
					if ($(this).is(":checked")) {
						vals.push($(this).val());
					}
				});
				$("#Workshop_IDs").val(vals);
				validateWorkshops();
			}
			var toggleInstitution = function() {
				var instType = $("#Institution_Type").val();
				if (instType == "High School") {
					$("#Institution_ID").val($("#High_School_ID").val());
					$("#collegeUniversityContainer").hide();
					$("#highSchoolContainer").show();
					$("#institutionContainer").hide();
					$("#departmentContainer").show();
					$("#address1Container").show();
					$("#address2Container").show();
					$("#cityZipCodeContainer").show();
				} else if (instType == "College University") {
					$("#Institution_ID").val($("#College_University_ID").val());
					$("#collegeUniversityContainer").show();
					$("#highSchoolContainer").hide();
					$("#institutionContainer").hide();
					$("#departmentContainer").show();
					$("#address1Container").show();
					$("#address2Container").show();
					$("#cityZipCodeContainer").show();
				} else {
					$("#Institution_ID").val(0);
					$("#collegeUniversityContainer").hide();
					$("#highSchoolContainer").hide();
					$("#institutionContainer").show();
					$("#departmentContainer").show();
					$("#address1Container").show();
					$("#address2Container").show();
					$("#cityZipCodeContainer").show();
				}
			}
			var putCollegeUniversityID = function() {
				var cuID = $("#College_University_ID").val();
				if (isInt(cuID)) {
					$("#Institution_ID").val(cuID);
				} else {
					$("#Institution_ID").val("");
				}
			}
			var putHighSchoolID = function() {
				var hsID = $("#High_School_ID").val();
				if (isInt(hsID)) {
					$("#Institution_ID").val(hsID);
				} else {
					$("#Institution_ID").val("");
				}
			}		
			$(function() {
				initScreen();
				toggleInstitution();
				setProductQuantityRules();
				$("#csacOrderForm").validate(validatorConfig);
				$("#csacOrderModal").modal();
				resizeModal("csacOrderModal");
			})
		//-->
		</script>
		<style>
			table tr td small {font-size: 0.8em;}
		</style>
		<cfform id="csacOrderForm" name="csacOrderForm" action="#script_name#" role="form" method="post">
			<cfoutput>
				<input type="hidden" id="action" name="action" value="saveCSACOrder" />
				<input type="hidden" id="view" name="view" value="#returnView#" />
				<input type="hidden" id="CSAC_Order_ID" name="CSAC_Order_ID" value="#csacOrder.getCSAC_Order_ID()#" />
				<input type="hidden" id="Program_Year" name="Program_Year" value="#csacOrder.getProgram_Year()#" />
				<input type="hidden" id="Order_ID" name="Order_ID" value="#csacOrder.getOrder_ID()#" />
				<input type="hidden" id="Organization_ID" name="Organization_ID" value="#csacOrder.getOrganization_ID()#" />
				<input type="hidden" id="Ordered_By_ID" name="Ordered_By_ID" value="#csacOrder.getOrdered_By_ID()#" />
				<input type="hidden" id="Institution_ID" name="Institution_ID" value="#csacOrder.getInstitution_ID()#" />
				<input type="hidden" id="Workshop_IDs" name="Workshop_IDs" value="#csacOrder.getWorkshop_IDs()#" />
				<input type="hidden" id="Total_Anticipated_Students" name="Total_Anticipated_Students" value="#totalAnticipatedStudents#" />
				<input type="hidden" id="Total_Anticipated_Volunteers" name="Total_Anticipated_Volunteers" value="#totalAnticipatedVolunteers#" />
				<input type="hidden" id="Total_Anticipated_Computers" name="Total_Anticipated_Computers" value="#totalAnticipatedComputers#" />
				<input type="hidden" id="Minimum_Unique_Volunteers" name="Minimum_Unique_Volunteers" value="#minimumUniqueVolunteers#" />
			</cfoutput>
			<div id="csacOrderModal" class="modal modal-fullscreen" data-backdrop="static">
				<div class="modal-header">
					<cfif csacOrder.getCSAC_Order_ID() eq 0>
						<h3>Order</h3>
					<cfelse>
						<cfoutput>
							<h3>Order ###csacOrder.getOrder_ID()#</h3>
						</cfoutput>
					</cfif>
				</div>
				<!--- modal body --->
				<div class="modal-body">
					<div class="container-fluid" style="background-color: white;">
						<div id="shippingInfoHeaderContainer" class="row-fluid screen screen1">
							<div class="span12">
								<h4>Shipping Information</h4>
								<p>Enter the contact information for the person who will receive this order shipment.  Please include any special shipping instructions including holidays when your organization is closed and cannot receive shipments.</p>
							</div>
						</div>
						<!--- screen 1 --->
						<div class="row-fluid screen screen1">
							<cfoutput>
								<div class="span4">
									<cfif IsUserInRole("Super User")>
										<div class="clearfix">
											<label for="Status" class="required">Status</label>
											<select id="Status" name="Status" class="span12" required>
												<cfloop index="s" from="1" to="#ArrayLen(statuses)#">
													<option value="#statuses[s].value#"<cfif csacOrder.getStatus() eq statuses[s].value> selected</cfif>>#statuses[s].text#</option>
												</cfloop>
											</select>
										</div>
									<cfelseif csacOrder.getCSAC_Order_ID() gt 0>
										<div class="clearfix">
											<label>Status</label>
											<input type="text" id="Status" name="Status" value="#csacOrder.getStatus()#" class="span12" disabled="disabled" />
										</div>
									</cfif>
									<div class="clearfix">
										<label for="First_Name" class="required">First Name</label>
										<input type="text" class="span12" id="First_Name" name="First_Name" value="#csacOrder.getFirst_Name()#" />
									</div>
									<div class="clearfix">
										<label for="Last_Name" class="required">Last Name</label>
										<input type="text" class="span12" id="Last_Name" name="Last_Name" value="#csacOrder.getLast_Name()#" />
									</div>
									<div class="clearfix">
										<label for="Email" class="required">E-mail</label>
										<input type="email" class="span12" id="Email" name="Email" value="#csacOrder.getEmail()#" />
									</div>
									<div class="clearfix">
										<div class="row-fluid">
											<div class="span9">
												<label for="Phone" class="required">Phone</label>
												<input type="tel" id="Phone" name="Phone" value="#csacOrder.getPhone()#" class="span12"  required placeholder="######-######-########" />
											</div>
											<div class="span3">
												<label for="Extension">Ext</label>
												<input type="text" id="Extension" name="Extension" value="#csacOrder.getExtension()#" class="span12">
											</div>
										</div>
									</div>
								</div>
								<div class="span4">
									<div class="clearfix">
										<label for="Institution_Type" class="required">Recipient Organization Type</label>
										<select id="Institution_Type" name="Institution_Type" class="span12" onChange="toggleInstitution();">
											<option value="">Select...</option>
											<cfloop index="t" from="1" to="#ArrayLen(institutionTypes)#">
												<option value="#institutionTypes[t].value#"<cfif csacOrder.getInstitution_Type() eq institutionTypes[t].value> selected</cfif>>#institutionTypes[t].text#</option>
											</cfloop>
										</select>
									</div>
									<div id="collegeUniversityContainer" class="clearfix" style="display: none;">
										<label for="College_University_ID" class="required">College/University</label>
										<select id="College_University_ID" name="College_University_ID" class="span12" onChange="putCollegeUniversityID()">
											<option value="">Select...</option>
											<cfloop query="institutions">
												<option value="#Institution_ID#"<cfif csacOrder.getInstitution_ID() eq Institution_ID> selected</cfif>>#Institution# - #FAO_City#</option>
											</cfloop>
										</select>
									</div>
									<div id="highSchoolContainer" class="clearfix" style="display: none;">
										<label for="High_School_ID" class="required">High School</label>
										<select id="High_School_ID" name="High_School_ID" class="span12" onChange="putHighSchoolID()">
											<option value="">Select...</option>
											<cfloop query="highSchools">
												<option value="#High_School_ID#"<cfif csacOrder.getInstitution_ID() eq High_School_ID> selected</cfif>>#High_School# - #City#</option>
											</cfloop>
										</select>
									</div>
									<div id="institutionContainer" class="clearfix" style="display: none;">
										<label for="Institution" class="required">Organization</label>
										<input type="text" class="span12" id="Institution" name="Institution" value="#csacOrder.getInstitution()#">
									</div>
									<div id="departmentContainer" class="clearfix" style="display: none;">
										<label for="Department">Department</label>
										<input type="text" class="span12" id="Department" name="Department" value="#csacOrder.getDepartment()#">
									</div>
									<div id="address1Container" class="clearfix" style="display: none;">
										<label for="Address1" class="required">Address 1</label>
										<input type="text" class="span12" id="Address1" name="Address1" value="#csacOrder.getAddress1()#" />
									</div>
									<div id="address2Container" class="clearfix" style="display: none;">
										<label for="Address2">Address 2</label>
										<input type="text" class="span12" id="Address2" name="Address2" value="#csacOrder.getAddress2()#" >
									</div>
									<div id="cityZipCodeContainer" class="clearfix" style="display: none;">
										<div class="row-fluid">
											<div class="span8">
												<label for="City" class="required">City</label>
												<input type="text" id="City" name="City" class="span12" value="#csacOrder.getCity()#" />
											</div>
											<div class="span4">
												<label for="Zip_Code" class="required">Zip Code</label>
												<input type="text" id="Zip_Code" name="Zip_Code" maxlength="5" class="span12" value="#csacOrder.getZip_Code()#" />
											</div>
										</div>
									</div>
								</div>
								<div class="span4">
									<div class="clearfix">
										<label for="Shipping_Instructions">Shipping&nbsp;Instructions</label>
										<textarea class="span12" id="Shipping_Instructions" name="Shipping_Instructions" rows="2" placeholder="Include any special instructions and dates your organization is closed. For example...Closed from 12/20 to 1/7">#csacOrder.getShipping_Instructions()#</textarea>
									</div>
									<div class="clearfix">
										<label for="Shared_Notes">Notes (include hours of operation and break/holiday dates)</label>
										<textarea class="span12" id="Shared_Notes" name="Shared_Notes" rows="2">#csacOrder.getShared_Notes()#</textarea>
									</div>
									<cfif IsUserInRole("Super User")>
										<div class="clearfix">
											<label for="Private_Notes">Private Notes</label>
											<textarea class="span12" id="Private_Notes" name="Private_Notes" rows="2">#csacOrder.getPrivate_Notes()#</textarea>
										</div>
									</cfif>
									<!--- <div class="text-control clearfix">
										<label for="Tracking">Tracking Codes</label>
										<cfif csacOrder.getCSAC_Order_ID() eq 0>
											Tracking codes will appear here when your order is shipped
										<cfelse>
											<cfif qTrackings.RecordCount gt 0>
												<cfloop query="qTrackings">
													<a href="##" onclick="alert('Integrate with FedEx online tracking'); return false;">#Tracking_Code#</a><br />
												</cfloop>
											<cfelse>
												No tracking codes
											</cfif>
										</cfif>
									</div> --->
									<div class="clearfix">
										<label>Organization</label>
										<input type="text" name="foo" value="#organization.getOrganization()#" class="span12" disabled="disabled" />
									</div>
									<div class="clearfix">
										<label>Ordered By</label>
										<input type="text" name="foo" value="#orderedBy.getFirst_Name()# #orderedBy.getLast_Name()#" class="span12" disabled="disabled" />
									</div>
								</div>
							</cfoutput>
						</div>
						<!--- screen 2 --->
						<div class="row-fluid screen screen2">
							<div class="span12">
								<h4>Workshops</h4>
								<p>Choose the workshops that are applicable to this order.  Include all workshops that should be delivered to the shipping address.  For workshops that have a different shipping address create a separate order.</p>
								<p>The amounts listed in anticipated students, volunteers and computer were provided at the time of registration.  Please update any amounts if your plans or resources have changed.</p>
								<table class="table table-striped table-bordered">
									<thead>
										<tr>
											<th colspan="2">Workshop</th>
											<th>Anticipated Students</th>
											<th>Anticipated Volunteers</th>
											<th>Anticipated Computers</th>
										</tr>
									</thead>
									<tbody>
										<cfoutput query="qOrderWorkshops" group="Site">
											<cfoutput>
												<tr>
													<td align="center" style="border-right-width: 0px;">
														<input type="checkbox" id="Workshop_ID_#Workshop_ID#" name="Workshop_ID" value="#Workshop_ID#" class="workshop" onchange="updateWorkshopIDs();" style="margin: 0; padding: 0;" <cfif ListFind(csacOrder.getWorkshop_IDs(), Workshop_ID) gt 0> checked="checked"</cfif> />
													</td>
													<td class="col100p" style="border-left-width: 0px;">
														#Site#<br />
														#DateFormat(Workshop_Date, "m/d/yyyy")# #TimeFormat(Start_Time, "h:mmtt")#-#TimeFormat(End_Time, "h:mmtt")#
													</td>
													<!--- <cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
														<td class="col120">#ListValueCount(usedWorkshopIDs, Workshop_ID)#</td>
													</cfif> --->
													<td class="col120">
														<input type="text" id="Anticipated_Students_#Workshop_ID#" name="Anticipated_Students_#Workshop_ID#" value="#Anticipated_Students#" class="anticipatedStudentsCount" onchange="validateWorkshops()" style="width: 80px;" />
													</td>
													<td class="col120">
														<input type="text" id="Anticipated_Volunteers_#Workshop_ID#" name="Anticipated_Volunteers_#Workshop_ID#" value="#Anticipated_Volunteers#" class="anticipatedVolunteersCount" onchange="validateWorkshops()" style="width: 80px;" />
													</td>
													<td class="col120">
														<input type="text" id="Anticipated_Computers_#Workshop_ID#" name="Anticipated_Computers_#Workshop_ID#" value="#Anticipated_Computers#" class="anticipatedComputersCount" onchange="validateWorkshops()" style="width: 80px;" />
													</td>
												</tr>
											</cfoutput>
										</cfoutput>
									</tbody>
								</table>
								<cfoutput>
									<cfif qOrderWorkshops.RecordCount gt 0>
										<div class="row-fluid">
											<div class="span12">
												<ul class="thumbnails">
													<li class="col110" style="margin-left: 0; margin-bottom: 10px;">
														<div class="thumbnail" style="padding: 12px;">
															<div id="totalAnticipatedStudents" class="anticipatedCount">#totalAnticipatedStudents#</div>
															<label>Total<br />Anticipated<br />Students</label>
														</div>
													</li>
													<li class="col120" style="margin-bottom: 10px;">
														<div class="thumbnail" style="padding: 12px;">
															<div id="totalAnticipatedVolunteers" class="anticipatedCount">#totalAnticipatedVolunteers#</div>
															<label>Total<br />Anticipated<br />Volunteers</label>
														</div>
													</li>
													<li class="col120" style="margin-bottom: 10px;">
														<div class="thumbnail" style="padding: 12px;">
															<div id="totalAnticipatedComputers" class="anticipatedCount">#totalAnticipatedComputers#</div>
															<label>Total<br />Anticipated<br />Computers</label>
														</div>
													</li>
													
												</ul>
											</div>
										</div>
											<div id="uniqueVolunteersContainer" class="row-fluid">
												<div class="span12">
													<label>How many of the Total Anticipated Volunteers listed above represent unique people?</label>
													<p class="help-block">Some volunteers help with more than one workshop.  Please enter the number of unique individuals that are represented in Total Anticipated Volunteers.</p>
													<input type="text" id="Unique_Volunteers" name="Unique_Volunteers" value="#csacOrder.getUnique_Volunteers()#" onchange="validateWorkshops()" style="width: 80px;" />
												</div>
											</div>
									</cfif>
								</cfoutput>
							</div>
						</div>
						<!--- screen 3 --->
						<div class="row-fluid screen screen3">
							<div class="span12">
								<h4>Materials</h4>
								<p>We've separated materials into the following categories:  CA Dream Act, items to promote your workshops, to use at your workshops, partner materials, and volunteer t-shirts. Because some items are more abundant, and others are more limited, we have added minimum and maximum quantity requirements to each item offered.  Adding these parameters will also help reduce the time it takes to approve requests.</p>
								<cfoutput query="qProducts" group="Category">
									<cfoutput group="Subcategory">
										<h5>#Category#<cfif Len(Subcategory) gt 0> - #Subcategory#</cfif></h5>
										<table class="table table-striped table-bordered">
											<thead>
												<tr>
													<th colspan="2">Item</th>
													<th class="col130">Quantity</th>
													<cfif IsUserInRole("Super User")>
														<th class="col130">Quantity Shipped</th>
													</cfif>
												</tr>
											</thead>
											<tbody>
												<cfoutput>
													<cfquery name="qOrderProduct" dbtype="query">
														select * from qOrderProducts where Product_ID = #Product_ID#
													</cfquery>
													<cfif qOrderProduct.RecordCount gt 0>
														<cfset opID = qOrderProduct.Order_Product_ID />
														<cfset qty = qOrderProduct.Quantity />
														<cfset qtyShipped = qOrderProduct.Quantity_Shipped />
													<cfelse>
														<cfset opID = 0 />
														<cfset qty = 0 />
														<cfset qtyShipped = 0 />
													</cfif>
													<input type="hidden" id="Order_Product_ID_#Product_ID#" name="Order_Product_ID" value="#opID#" class="orderProductID" />
													<input type="hidden" id="Product_ID_#Product_ID#" name="Product_ID" value="#Product_ID#" class="productID" />
													<cfset productImages = csacBase.getProductImages(productIDs = Product_ID) />
													<cfset productFiles = csacBase.getProductFiles(productIDs = Product_ID) />
													<tr>
														<!--- thumbnail, name/description --->
														<td class="col100p" style="vertical-align: top; border-right-width: 0;">
															<cfif productImages.RecordCount gt 0 and NOT IsUserInRole("Super User") and Find(cgi.SCRIPT_NAME, "/admin/") gt 0>
															
																<cfimage action="info" source="#productImages.Directory#\#productImages.Name#" structname="imageProps">
																<cfset w = 70 />
																<cfset wm = imageProps.width/w />
																<cfset h = imageProps.height/wm />
																<a href="http://www.publications.csac.ca.gov/common/images/products/#productImages.Name#" target="_blank"><img src="http://www.publications.csac.ca.gov/common/images/products/#productImages.Name#" width="#w#" height="#h#" align="left" style="margin-right: 10px;" /></a>
															</cfif>
															#Product#<br />
															<small><cfif Len(Trim(Description)) gt 0>#Description#</cfif><cfif Len(Trim(ID)) gt 0> Item ID #ID#.</cfif></small>
														</td>
														<!--- download icon --->
														<td class="col80" style="text-align: center; vertical-align: middle; border-left-width: 0;">
															<cfif productFiles.RecordCount gt 0>
																<a href="http://www.publications.csac.ca.gov/common/files/products/#productFiles.Name#" target="_blank"><span class="icon-download-alt" style="font-size: 3em;"></span></a><br />
																<small>Download</small>
															<cfelseif Download_Only eq 1>
																<a href="##" onclick="alert('File currently unavailable.\n\nPlease check back.'); return false;"><span class="icon-download-alt" style="font-size: 3em;"></span></a><br />
																<small>Download</small>
															</cfif>
														</td>
														<!--- quanity, quantity shipped --->
														<td style="text-align: center; vertical-align: middle;">
															<cfif qty gt 0>
																<input type="text" id="Quantity_#Product_ID#" name="Quantity_#Product_ID#" value="#qty#" class="form-control quantity" style="width: 80px;" />
																<div class="productQuantityRuleContainer_#Product_ID#" style="font-size: 0.8em; line-height: normal; display: none;"></div>
															<cfelseif Download_Only eq 1>
																<input type="hidden" id="Quantity_#Product_ID#" name="Quantity_#Product_ID#" value="#qty#" class="quantity" />
																<small>Download only</small>
															<cfelseif Inventory eq 1 and Stock lte 0 and Allow_Back_Order eq 0>
																<input type="hidden" id="Quantity_#Product_ID#" name="Quantity_#Product_ID#" value="#qty#" class="quantity" />
																<small>Out of stock</small>
															<cfelse>
																<input type="text" id="Quantity_#Product_ID#" name="Quantity_#Product_ID#" value="#qty#" class="form-control quantity" style="width: 80px;" />
																<div class="productQuantityRuleContainer_#Product_ID#" style="font-size: 0.8em; line-height: normal; display: none;"></div>
															</cfif>
														</td>
														<input type="hidden" id="Quantity_Shipped_#Product_ID#" name="Quantity_Shipped_#Product_ID#" value="#qtyShipped#" class="quantityShipped" />
														<cfif IsUserInRole("Super User")>
															<td style="text-align: center; vertical-align: middle;">
																#qtyShipped#
															</td>
														</cfif>
													</tr>
												</cfoutput>
											</tbody>
										</table>
									</cfoutput>
								</cfoutput>
							</div>
						</div>
					</div>
				</div>
				<!--- modal footer --->
				<div class="modal-footer">
					<cfoutput>
						<a id="cancelButton" href="?view=#returnView#&#passbackQueryString#" class="btn btn-success screenButton">Cancel</a>
						<a id="closeButton" href="?view=#returnView#&#passbackQueryString#" class="btn btn-success screenButton">Close</a>
						<a id="backButton" href="##" class="btn btn-success screenButton" onclick="previousScreen(); return false;">Back</a>
						<a id="nextButton" href="##" class="btn btn-success screenButton" onclick="nextScreen(); return false;">Next</a>
						<button id="saveButton" type="submit" name="saveButton" class="btn btn-success screenButton">Submit</button>
					</cfoutput>
				</div>
			</div>
		</cfform>
	<cfelse>
		<script type="text/javascript">
			$(function() {
				$("#noWorkshopsModal").modal();
				resizeModal("noWorkshopsModal");	
			})
		</script>
		<cfoutput>
			<div id="noWorkshopsModal" class="modal modal-fullscreen" data-backdrop="static">
				<div class="modal-header">
					<h3>No Workshops</h3>
				</div>
				<div class="modal-body">
					<div class="container-fluid">
						<div class="row-fluid">
							<div class="span12">
								<h4>No workshops available for order</h4>
								
									<cfif IsUserInRole("Super User") or IsUserInRole("Regional Admin")>
										<p>There are no workshops registered for this organization.</p>
									<cfelse>
										<p>To place an order there must be at least one registered workshop that meets both of the following conditions:</p>
										<ol>
											<li>Occurs in the future</li>
											<li>Has not already been submitted with an order</li>
										</ol>
									</cfif>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<a href="?view=#returnView#&#passbackQueryString#" class="btn btn-success">Close</a>
				</div>
			</div>
		</cfoutput>
	</cfif>
<cfelse>
	<div class="alert alert-info notFound">Order not found</div>
</cfif>
